package com.leimo.java_socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端
 *
 * @author 段誉
 * @create 2019-02-26 13:56
 */
public class MySocketClient {
  public static void main(String [] args)
  {
    int port = 9998;
    try
    {
      System.out.println("连接到主机：localhost，端口号：" + port);
      Socket client = new Socket("127.0.0.1", port);
      System.out.println("远程主机地址：" + client.getRemoteSocketAddress());
      OutputStream outToServer = client.getOutputStream();
      DataOutputStream out = new DataOutputStream(outToServer);

      out.writeUTF("Hello from " + client.getLocalSocketAddress());
      InputStream inFromServer = client.getInputStream();
      DataInputStream in = new DataInputStream(inFromServer);
      System.out.println("服务器响应： " + in.readUTF());
      client.close();
    }catch(IOException e)
    {
      e.printStackTrace();
    }
  }
}
